define(['angularAMD'], function (angularAMD) {
        angularAMD.service('B0212Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog','$mddProvider',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog,$mddProvider){
            this.init = function(options){
                var shell;
                // 需要使用外部瀏覽器開啟的連結前綴清單
                var externalLinkPrefixList = ['http'];
    
                try {
                    shell = window.nodeRequire('electron').shell;
                } catch(e) {
                    shell = null;
                }
               if(options){
                    options = {
                        plot_no: options.plot_no || '',
                        op_no:  options.op_no || '',
                        esop_detail: options.esop_detail ||[]
                    };
               } else {
                    options = {plot_no: '', op_no: '', esop_detail: []};
               }
    
                function zoom(magnification){
                    var kmiImgPanel = document.getElementById('kmi-img-panel'),
                        kmiImgContent = document.getElementById('kmi-img-content');
                    var after_width = kmiImgPanel.width*magnification,
                        after_height = kmiImgPanel.height*magnification;
                    var after = [(after_width-kmiImgPanel.width)/2, (after_height-kmiImgPanel.height)/2];
                    kmiImgPanel.style.width = after_width + 'px';
                    kmiImgPanel.style.height = after_height + 'px';
                    kmiImgContent.scrollLeft = kmiImgContent.scrollLeft+after[0];
                    kmiImgContent.scrollTop = kmiImgContent.scrollTop+after[1];
                }
    
                function esopInfoGet(callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.esop_info_get',
                        content : {
                            plot_no : options.plot_no,
                            op_no : options.op_no
                        },
                        success : function(data){
                            if(data.detail.length>0){
                                if (callback)
                                    callback(data.detail);
                            } else {
                              
                              
                                $rootScope.showAlert($filter('translate')('B0212.showAlert.no_data'));
                            }
                        }
                    });
                }
    
                function beforeOpenB0212(callback){
                    if(options.plot_no != '' && options.op_no != ''){
                        esopInfoGet(function(esopList){
                            if (callback)
                                callback(esopList);
                        });
                    } else if(options.esop_detail.length > 0){
                        if (callback)
                            callback(options.esop_detail);
                    } else {
                        $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                    }
                }
    
                var openB0212 = function(esopList){
                        $mddProvider.UseForm('B0212', () => {
                                $mdDialog.dialog(
                                        'module/moduleB/program/B02/B0212/B0212.html',
                                        function(dialog){
                                            return {
                                                css_url:'module/moduleB/program/B02/B0212/B0212.css',
                                                esopList: esopList,
                                                isListOpen:true,
                                                currentFile:{},
                                                //20170626 modify by Dustdusk for 開啟後預帶第一筆
                                                beforeShown: function(){
                                                    dialog.clickEsopFile(dialog.esopList[0]);
                                                },
                                                closeList:function(){
                                                    this.isListOpen = !this.isListOpen;
                                                },
                                                clickEsopFile : function(esopFile){
                                                    dialog.currentFile = {esop_content:esopFile.esop_content, content_type : esopFile.content_type};
                                                    if(dialog.currentFile.esop_content.indexOf('file://') != -1){
                                                        dialog.currentFile.esop_content = dialog.currentFile.esop_content.replace('file://', '');
                                                    } else if(dialog.currentFile.esop_content.indexOf('http://') == -1){
                                                        dialog.currentFile.esop_content = 'http://'+config.server.ip+':'+ config.server.port + dialog.currentFile.esop_content;
                                                    }
                    
                                                    if(dialog.currentFile.content_type == '0'){
                                                        //PDF
                                                        document.getElementById('kmi-pdf-panel').src = 'JSplugins/pdfjs/web/viewer.html?file='+dialog.currentFile.esop_content
                                                            +'&openfile=false&viewbookmark=false&printfile=false&download=false#zoom=page-width';
                    
                                                        // 20220315 modify by Alan for M#106738: 使用外部程式開啟PDF檔案內的連結
                                                        // 20220408 modify by Alan for M#106738: 修正連結開啟異常
                                                        var pdfElem = document.getElementById('kmi-pdf-panel');
                    
                                                        pdfElem.addEventListener('load', function() {
                                                            var pdfFrame = window.frames['kmi-pdf-panel']
                                                            pdfFrame.PDFViewerApplication.preferences.set('externalLinkTarget', 2);
                                                            
                                                            if (!shell) return
                                                            pdfFrame.document.addEventListener('pagerendered', function() {
                                                                var links = this.querySelectorAll('.linkAnnotation a[href]');
                                                                links.forEach(function(link) {
                                                                    if (link.getAttribute('listener') === 'true') return
                    
                                                                    link.setAttribute('listener', 'true');
                                                                    link.addEventListener('click', function(e) {
                                                                        e.preventDefault();
                                                                        var href = e.target.getAttribute('href');
                                                                        var isExternalLink = externalLinkPrefixList.find((prefix) => href.startsWith(prefix));
                                                                        if (isExternalLink) {
                                                                            shell.openExternal(e.target.href);
                                                                        }
                                                                    })
                                                                })
                                                            });
                                                        })                                
                                                    } else if(dialog.currentFile.content_type == '1'){
                                                        //影片
                                                        var VideoPanel = document.getElementById('kmi-video-panel');
                                                        VideoPanel.innerHTML = '';
                                                        VideoPanel.innerHTML = "<source src='"+dialog.currentFile.esop_content+"' type=\"video/mp4\">";
                                                        //VideoPanel.innerHTML = "<source src='http://gallery.mjes.ntpc.edu.tw/moodle/pluginfile.php/15477/mod_resource/content/3/4%E6%98%9F%E6%98%9F%E4%B8%80%E5%A4%A9%E7%9A%84%E9%81%8B%E8%A1%8C%20%281%29.mp4' type=\"video/mp4\">";
                                                        // 20200727 modify by WeiTing for M#75510: ESOP切換影片時，需要重新 load，不然會無法切換。
                                                        VideoPanel.load();
                                                    } else if(dialog.currentFile.content_type == '2'){
                                                        //圖片
                                                        //dialog.currentFile.esop_content = "https://i.stack.imgur.com/iIvCA.png";
                                                        var kmiImgPanel = document.getElementById('kmi-img-panel');
                                                       
                                                        kmiImgPanel.style.width = '100%';
                                                        kmiImgPanel.style.height = '100%';
                                                    } else if(dialog.currentFile.content_type == '3'){
                                                        //網頁
                                                        //dialog.currentFile.esop_content = "https://i.stack.imgur.com/iIvCA.png";
                                                         // 20231010 modify by shiwl for M#0147529: 测试区读取到PLM图纸后，文件不显示，但PLM端可以正常打开
                                                         setTimeout(() => {
                                                            document.getElementById('kmi-pdf-panel').src =  dialog.currentFile.esop_content; 
                                                        }, 500);
                    
                                                    } else {
                                                        //
                                                    }
                                                },
                                                                            zoom_in : function(){
                                                    if(dialog.currentFile.content_type == '0'){
                                                        window.frames['kmi-pdf-panel'].zoom_in();
                                                    } else if(dialog.currentFile.content_type == '2'){
                                                        zoom(1.1);
                                                    }
                                                                            },
                                                                            zoom_out : function(){
                                                    if(dialog.currentFile.content_type == '0'){
                                                        window.frames['kmi-pdf-panel'].zoom_out();
                                                    } else if(dialog.currentFile.content_type == '2'){
                                                                                        zoom(0.9);
                                                    }
                                                                            },
                                                back: function () {
                                                    dialog.hide();
                                                }
                                            };
                                        }
                                    );
                        })
                    
                };
    
                beforeOpenB0212(function(esopList){
                   
                    openB0212(esopList);
                });
            };
        }]);
    });
    